﻿using DevExpress.XtraEditors;
using MDK.Device.Devices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDK.Winform.UserControl.Config
{
    public partial class Centrifugal : BaseForm
    {
        private Centrifuge centrifuge;

        #region 初始化
        public Centrifugal()
        {
            InitializeComponent();
        }

        private void Centrifugal_Load(object sender, EventArgs e)
        {
            Thread th = new Thread(RefreshDeviceStatus);
            th.IsBackground = true;
            th.Start();
        }

        private void ckLeft_CheckedChanged(object sender, EventArgs e)
        {
            ckRight.Checked = !ckLeft.Checked;

            if (ckLeft.Checked)
            {
                centrifuge = App.flow.SysModel.Centrifuges[0];
            }
        }

        private void ckRight_CheckedChanged(object sender, EventArgs e)
        {
            ckLeft.Checked = !ckRight.Checked;

            if (ckRight.Checked)
            {
                centrifuge = App.flow.SysModel.Centrifuges[1];
            }
        }
        #endregion

        #region 刷新设备状态
        public void RefreshDeviceStatus()
        {
            while (true)
            {
                try
                {
                    Thread.Sleep(1000);
                    if (ckLeft.Checked != ckRight.Checked)
                    {
                        if (this.IsHandleCreated)
                        {
                            txtCurrentSpeed.Invoke(new MethodInvoker(() =>
                        {
                            txtCurrentSpeed.Text = centrifuge.CurrentSpeed.ToString();
                        }));
                            txtElapseTime.Invoke(new MethodInvoker(() =>
                            {
                                txtElapseTime.Text = centrifuge.ElapseTime.ToString();
                            }));
                            txtSurplusTime.Invoke(new MethodInvoker(() =>
                            {
                                txtSurplusTime.Text = centrifuge.SurplusTime.ToString();
                            }));
                            circle1.Invoke(new MethodInvoker(() =>
                            {
                                if (centrifuge.GetDoorState() == CentrifugeDoorLocations.Opened)
                                {
                                    circle1.Set2();
                                }
                                else if (centrifuge.GetDoorState() == CentrifugeDoorLocations.Closed)
                                {
                                    circle1.Set1();
                                }
                                else
                                {
                                    circle1.Set3();
                                }
                            }));
                            circle2.Invoke(new MethodInvoker(() =>
                            {
                                if (centrifuge.CompressorIsOn())
                                {
                                    circle2.Set2();
                                }
                                else
                                {
                                    circle2.Set1();
                                }
                            }));
                        }
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

        #endregion

        private void txtTemperatureRead_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                txtTemperatures.Text = centrifuge.Temperature.ToString();
        }

        private void txtTemperatureWrite_Click(object sender, EventArgs e)
        {
            double i = 0;
            double.TryParse(txtTemperatures.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                if (centrifuge != null)
                    centrifuge.Temperature = i;
            }
        }

        private void btnSpeedRead_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                txtSpeed.Text = centrifuge.Speed.ToString();
        }

        private void btnSpeedWrite_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtSpeed.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                if (centrifuge != null)
                    centrifuge.Speed = i;
            }
        }

        private void btnBalanceRead_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                txtBalance.Text = centrifuge.Balance.ToString();
        }

        private void btnBalanceWrite_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtBalance.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                if (centrifuge != null)
                    centrifuge.Balance = i;
            }
        }

        private void btnTimeRead_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                txtTime.Text = centrifuge.RunTime.ToString();
        }

        private void btnTimeWrite_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtTime.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                if (centrifuge != null)
                    centrifuge.RunTime = i;
            }
        }

        private void btnDoorOpen_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                centrifuge.StartOpenDoor();
        }

        private void btnDoorClose_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                centrifuge.StartCloseDoor();
        }

        private void btnSationRead_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                txtSation.Text = centrifuge.GetStation().ToString();
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                centrifuge.GotoStation(0);
        }

        private void btnGoSation_Click(object sender, EventArgs e)
        {
            int i = 0;
            int.TryParse(txtSation.Text.Trim(), out i);
            if (i == 0)
            {
                FastMessageBox.Show("请确认输入格式");
            }
            else
            {
                if (centrifuge != null)
                    centrifuge.GotoStation(i);
            }
        }

        private void btnOpenCentrifuge_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                centrifuge.Begin();
        }

        private void btnStopCentrifuge_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                centrifuge.Stop();
        }

        private void btnOpenCompression_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                centrifuge.TurnOnCompressor();
        }

        private void btnStopCompression_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                centrifuge.TurnOffCompressor();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            if (centrifuge != null)
                centrifuge.ClearError();
        }
    }
}
